import storage from '@/utils/storage'
const HEADERTABS = 'back-header-tabs'

export interface Tab {
  name: string
  path: string
}

const store = {
  state: {
    // 放数据
    tabs: storage.getStorage(HEADERTABS) || [{ name: '首页', path: '/home' }]
  },
  actions: {
    // 放同步方法
    // 保存Tabs
    saveTabs(newState: { tabs: Tab[] }, action: { type: string; val: Tab[] }) {
      newState.tabs = action.val
      storage.setStorage(HEADERTABS, newState.tabs)
    },
    // 添加Tabs
    addTabs(newState: { tabs: Tab[] }, action: { type: string; val: Tab }) {
      const tabs: Tab[] = storage.getStorage(HEADERTABS) || [
        { name: '首页', path: '/home' }
      ]
      !tabs.find((item: Tab) => item.path === action.val.path) &&
        tabs.push(action.val)

      newState.tabs = tabs
      storage.setStorage(HEADERTABS, newState.tabs)
    }
  },
  asyncActions: {
    // 放异步方法
  },
  actionNames: {}
}
let actionNames = {}
for (let key in store.actions) {
  actionNames[key] = key
}
store.actionNames = actionNames

export default store
